package cn.itsource.basic.controller;

import cn.itsource.basic.dto.BinderDto;
import cn.itsource.basic.dto.LoginDto;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.ILoginService;
import cn.itsource.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;
    /**
     * 用户名密码登录
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto){
        try {
            HashMap<String, Object> map = loginService.account(loginDto);
            return AjaxResult.me().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        }
    }



    /**
     * 微信登录
     * @param map
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String,String> map){
        try {
            //可能是false 可能是true
            return loginService.wechat(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        }
    }

    /*
    绑定接口
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody BinderDto binderDto){
        try {
            HashMap<String, Object> map =loginService.binder(binderDto);
            return AjaxResult.me().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败！"+e.getMessage());
        }
    }
}
